import http from '../http'
import qs from 'qs'
import { request, requestOnline, ResponseCode } from '../baseUrl'

/**
 *  @param request 请求地址 例如：http://197.82.15.15:8088/request/...
 *  @param '/testIp'代表vue-cil中config，index.js中配置的代理
 */

/**
 * @func 子账号列表
 * @param {列表对象: record}
 */
const subUserList = `/subUser/list`
export async function getSubUserList({ pageNo = 1, pageSize = 100 }) {
  try {
    const response = await http.get(`${requestOnline}${subUserList}?${qs.stringify({ pageNo, pageSize })}`)
    if (ResponseCode.success !== response.data.code) {
      const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
      throw new Error(message)
    }
    return [null, response.data.data]
  } catch (error) {
    return [(typeof error.message === 'string' && error.message) || 'error', null]
  }
}

/**
 *@开启子账号
 */
const subUserOpen = `/subUser/open`
export function getSubUserOpen(id) {
  return http.post(`${requestOnline}${subUserOpen}`, qs.parse(id))
}

/**
 *@停用子账号
 */
const subUserStop = `/subUser/stop`
export function getSubUserStop(id) {
  return http.post(`${requestOnline}${subUserStop}`, qs.parse(id))
}

/**
 *@新增子账号
 */
const subUserAdd = `/subUser/add`
export function postSubUserAdd(param) {
  return http.post(`${requestOnline}${subUserAdd}`, qs.parse(param))
}

/**
 *@func 子账号充值{充值}
 */
const subUserDeposit = `/subUser/deposit`
export async function getSubUserDeposit(param) {
  const response = await http.post(`${requestOnline}${subUserDeposit}`, qs.parse(param))
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 子账号充值{查看充值记录}
 * @param {列表对象: record }
 */
const subUserBalanceOrder = `/subUser/balance/order`
export async function getSubUserBalanceOrder({ pageNo = 1, pageSize = 100, subUserId }) {
  const response = await http.get(`${requestOnline}${subUserBalanceOrder}?${qs.stringify({ pageNo, pageSize, subUserId })}`)
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 编辑账号更新
 * @param {列表对象: record }
 */
const subUserUpdate = `/subUser/update`
export async function postSubUserUpdate(param) {
  const response = await http.post(`${requestOnline}${subUserUpdate}`, qs.parse(param))
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}

/**
 * @func 广告子账号列表
 */
const subUserListAdvert = `/subUser/list/advert`
export async function getSubUserListAdvert() {
  const response = await http.get(`${requestOnline}${subUserListAdvert}`)
  if (ResponseCode.success !== response.data.code) {
    const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
    throw new Error(message)
  }
  return response.data.data
}
